using System;
using System.Collections.Generic;
using System.Linq;
using Engine;
using Game;

namespace RuthlessConquest {
    public class GiftOrderDialog : ListSelectionDialog {
        public const string fName = "RCGiftOrderDialog";

        public class OrderWidget : StackPanelWidget {
            public HumanPlayer Player { get; }
            public Planet TargetPlanet { get; }
            public IEnumerable<Planet> SelectedPlanets { get; }
            public int OrderIndex { get; }
            public LabelWidget Label1 { get; }
            public LabelWidget Label2 { get; }

            public OrderWidget(HumanPlayer player, Planet targetPlanet, IEnumerable<Planet> selectedPlanets, int orderIndex) {
                HorizontalAlignment = WidgetAlignment.Center;
                VerticalAlignment = WidgetAlignment.Center;
                Player = player;
                TargetPlanet = targetPlanet;
                SelectedPlanets = selectedPlanets;
                OrderIndex = orderIndex;
                Direction = LayoutDirection.Vertical;
                Children.Add(Label1 = new LabelWidget { HorizontalAlignment = WidgetAlignment.Center });
                Label2 = new LabelWidget { FontScale = 0.75f, Color = Colors.ForeDisabled, HorizontalAlignment = WidgetAlignment.Center };
                Children.Add(Label2);
            }

            public override void MeasureOverride(Vector2 parentAvailableSize) {
                string format = LanguageControl.Get(fName, SettingsManager.DisplayOptimalResultOfShipSent ? "6" : "2");
                int sum = SelectedPlanets.Where(p => p != TargetPlanet).Sum(p => p.ShipsCount);
                switch (OrderIndex) {
                    case 0:
                        Label1.Text = LanguageControl.Get(fName, "1");
                        Label2.Text = SettingsManager.DisplayOptimalResultOfShipSent
                            ? string.Format(
                                format,
                                sum,
                                $"{TargetPlanet.ShipsCount}→{Math.Min(TargetPlanet.ShipsCount + sum, Planet.MaxShipsCapacity)}"
                            )
                            : string.Format(format, sum);
                        break;
                    case 1:
                        Label1.Text = LanguageControl.Get(fName, "3");
                        Label2.Text = SettingsManager.DisplayOptimalResultOfShipSent
                            ? string.Format(
                                format,
                                sum / 2,
                                $"{TargetPlanet.ShipsCount}→{Math.Min(TargetPlanet.ShipsCount + sum / 2, Planet.MaxShipsCapacity)}"
                            )
                            : string.Format(format, sum / 2);
                        break;
                    case 2:
                        Label1.Text = LanguageControl.Get(fName, "4");
                        Label2.Text = SettingsManager.DisplayOptimalResultOfShipSent
                            ? string.Format(
                                format,
                                sum / 5,
                                $"{TargetPlanet.ShipsCount}→{Math.Min(TargetPlanet.ShipsCount + sum / 5, Planet.MaxShipsCapacity)}"
                            )
                            : string.Format(format, sum / 5);
                        break;
                }
                base.MeasureOverride(parentAvailableSize);
            }
        }

        public HumanPlayer Player;

        public Planet TargetPlanet;

        public GiftOrderDialog(HumanPlayer player, Planet targetPlanet, IEnumerable<Planet> selectedPlanets) : base(
            LanguageControl.Get(fName, "5"),
            CreateItems(player, targetPlanet, selectedPlanets),
            68f,
            o => new OrderWidget(player, targetPlanet, selectedPlanets, (int)o),
            null
        ) {
            GiftOrderDialog giftOrderDialog = this;
            SelectionColor = global::RuthlessConquest.Player.GetColor(player.Faction);
            Player = player;
            TargetPlanet = targetPlanet;
            ContentSize = new Vector2(420f, 300f);
            SelectionHandler = (Action<object>)Delegate.Combine(
                SelectionHandler,
                (Action<object>)delegate(object o) {
                    switch ((int)o) {
                        case 0:
                            giftOrderDialog.Player.IssueMoveOrder(
                                giftOrderDialog.TargetPlanet,
                                selectedPlanets,
                                1f,
                                giftOrderDialog.TargetPlanet.Faction
                            ); break;
                        case 1:
                            giftOrderDialog.Player.IssueMoveOrder(
                                giftOrderDialog.TargetPlanet,
                                selectedPlanets,
                                0.5f,
                                giftOrderDialog.TargetPlanet.Faction
                            ); break;
                        case 2:
                            giftOrderDialog.Player.IssueMoveOrder(
                                giftOrderDialog.TargetPlanet,
                                selectedPlanets,
                                0.2f,
                                giftOrderDialog.TargetPlanet.Faction
                            ); break;
                    }
                }
            );
        }

        public static IEnumerable<int> CreateItems(HumanPlayer player, Planet targetPlanet, IEnumerable<Planet> selectedPlanets) {
            yield return 0;
            yield return 1;
            yield return 2;
        }
    }
}